﻿using System;
using System.IO;
using NUnit.Framework;

namespace SvnDeploy.Service.Impl
{
    [TestFixture]
    public class ZipAdapterImplTests
    {
        private string testDirectory;
        private string dummyFile;
        private IZipAdapter zipAdapter;
        private string zipPath;

        [SetUp]
        public void BeforeEachTest()
        {
            
            testDirectory = @"C:\this is a test directory " + Guid.NewGuid();
            Directory.CreateDirectory(testDirectory + @"\path_to_zip");
            dummyFile = testDirectory + @"\path_to_zip\dummy_file.txt";
            CreateFile(dummyFile);
            zipAdapter = new ZipAdapterImpl();
            zipPath = testDirectory + @"\zipped.zip";
        }

        [TearDown]
        public void AfterEachTest()
        {
            Directory.Delete(testDirectory, true);
        }

        [Test]
        public void ZipFolder_CreatesZipDirectory()
        {
            zipAdapter.ZipFolder(testDirectory + @"\path_to_zip", zipPath);
            var fileInfo = new FileInfo(zipPath);

            Assert.IsTrue(File.Exists(zipPath));
            Assert.IsTrue(fileInfo.Length > 0);
            Assert.AreEqual(".zip", fileInfo.Extension);
        }

        private static void CreateFile(string path)
        {
            using (var file = new FileStream(path, FileMode.OpenOrCreate))
            {
                using (var writer = new StreamWriter(file))
                {
                    writer.Write("This is a sample file.");
                }
            }
        }
    }
}
